package com.example.api.gateway.filter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * 处理post异常
 * @author c_dingxuehua-001
 *
 */
//@Component
public class ErrorExtFilter extends SendErrorFilter{

	@Override
	public Object run() {
		RequestContext requestContext = RequestContext.getCurrentContext();
		Throwable throwable = requestContext.getThrowable();
		requestContext.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		requestContext.set("error_exception", throwable.getCause());
		return null;
	}

	@Override
	public boolean shouldFilter() {
		RequestContext requestContext = RequestContext.getCurrentContext();
		ZuulFilter failedFilter = (ZuulFilter) requestContext.get("failed.filter");
		if(failedFilter != null &&failedFilter.filterType().equals("post")){
			return true;
		}
		return true;
	}

	@Override
	public int filterOrder() {
		return 30;
	}

	@Override
	public String filterType() {
		return "error";
	}
	
}
